package com.jnu.student.myfirstapplication.ui.award;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.jnu.student.myfirstapplication.R;

public class AddAwardActivity extends AppCompatActivity {

    int position = -1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_award);
        Intent intent_incoming = getIntent();

        if (intent_incoming != null) {
            String name = intent_incoming.getStringExtra("name");
            if (null != name) { //"修改"传入数据
                position = intent_incoming.getIntExtra("position",-1);
                int score = intent_incoming.getIntExtra("score", -1);
                EditText editTextName = findViewById(R.id.addAwardName);
                editTextName.setText(name);
                EditText editTextScore = findViewById(R.id.addAwardScore);
                editTextScore.setText(Integer.toString(score));
            }
            else {  // "添加"
            }
        }
        // 数据传出以及切换activity
        Button buttonOK = findViewById(R.id.addAwardOK);
        buttonOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                EditText editTextName= findViewById(R.id.addAwardName);     // 获取数据
                EditText editTextScore = findViewById(R.id.addAwardScore);

                intent.putExtra("name", editTextName.getText().toString());    // 传回数据
                intent.putExtra("score", Integer.parseInt(editTextScore.getText().toString()));
                setResult(Activity.RESULT_OK, intent);
                AddAwardActivity.this.finish();
            }
        });
        Button buttonCancel = findViewById(R.id.addAwardCancle);
        buttonCancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setResult(Activity.RESULT_CANCELED, new Intent());
                AddAwardActivity.this.finish();
            }
        });
    }
}